package sk.tramtatiri.pages.users;


import java.util.Arrays;
import java.util.List;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.MarkupStream;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.FormComponent;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.Model;
import org.apache.wicket.spring.injection.annot.SpringBean;
import sk.tramtatiri.core.IConstants;
import sk.tramtatiri.data.dao.interfaces.SportDao;
import sk.tramtatiri.data.dataobjects.Food;
import sk.tramtatiri.data.dataobjects.Sport;


public class SportCreatePanel extends Panel {

	private static final long serialVersionUID = 9068403964361464535L;

	@SpringBean
	private SportDao sportDao;

	private String result;
	private TextField<String> sportName;
	private TextField sportCalories;
	private FeedbackPanel feedback;
	private DropDownChoice<Integer> sportDifficulty;
	private Label addMessage;


	public SportCreatePanel(String id) {
		super(id);
        
        sportName = new TextField<String>("sportName", Model.of("[Zadajte meno sportu]"));
        sportName.setRequired(true);
        sportName.setLabel(new Model<String>("sportName"));
        
        sportCalories = new TextField("sportCalories", Model.of("[Zadajte kalorie pre sport]"), Integer.class);
        sportCalories.setRequired(true);
        sportCalories.setLabel(new Model<String>("sportCalories"));
        
        sportDifficulty = new DropDownChoice<Integer>("sportDifficulty", new Model("") ,IConstants.SPORT_DIFFICULTY);// mozno iny konstruktor s listom moznosti, neviem
        sportDifficulty.setRequired(true);
        sportDifficulty.setLabel(new Model<String>("sportDifficulty")); 
        
        feedback = new FeedbackPanel("feedbackForSport");
        feedback.setOutputMarkupId(true);
        
        
        addMessage = new Label("addMessage",""); // pretoze nemoze byt null
        
		Form<?> form = new Form<Void>("formSportCreate");

		form.add(new AjaxButton("addSport") { 
  
			private static final long serialVersionUID = 4144535803282461571L;
			

			@Override
			protected void onSubmit(AjaxRequestTarget target, Form<?> arg1) {
				System.out.println("Kontrola: "+sportName.getInput()+" "+sportCalories.getInput());
				Sport sport = sportDao.findName(sportName.getInput());
				if(sport.equals(null)) {
					
					Sport newSport = new Sport();
					newSport.setName(sportName.getInput());
					newSport.setCalories(Integer.valueOf(sportCalories.getInput()));
					newSport.setRating(0); /** minimal rating first */
					newSport.setDifficulty(Integer.valueOf(sportDifficulty.getInput())); 
					sportDao.save(newSport);
					
					addMessage = new Label("successAdd","Sport bol uspesne pridany, dakujeme");
					addMessage.setOutputMarkupId(true);
					target.addComponent(addMessage);
				} else {
					//TODO cervenym text, alebo vypis toho ze existuje zaznam
					addMessage = new Label("addMessage","Sport nebol pridany");
					addMessage.setOutputMarkupId(true);
					target.addComponent(addMessage);
				}
			}
		});  
				
		form.add(sportName);
		form.add(sportCalories);
		form.add(sportDifficulty);
		
		add(feedback);
		add(addMessage);
		add(form);
    }

	/**
	 * @return the result
	 */
	public String getResult() {
		return result;
	}

	/**
	 * @param result
	 *            the result to set
	 */
	public void setResult(String result) {
		this.result = result;
	}

}
